package com.cjst.config;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.itextpdf.text.log.SysoCounter;
import com.lowagie.text.*;
import com.lowagie.text.Image;
import com.lowagie.text.rtf.RtfWriter2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.awt.Point;
import java.io.*;
import java.util.List;


public class CreateWord {
	public static void createDocContext(List<Image> imageList,HttpServletRequest request, HttpServletResponse response,String imgLocation) throws DocumentException, IOException {
        File file1 = new File(imgLocation+"/ss.doc");
        //judeFileExists(file1);
        Document document = new Document();
        // 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
        RtfWriter2.getInstance(document, new FileOutputStream(file1));
        document.open();
        //Paragraph title = new Paragraph("登录二维码信息");
        //设置标题格式对齐方式
        //title.setAlignment(Element.ALIGN_CENTER);
        //document.add(title);
        // 设置 Table 表格
        //设置表格，将图片加到表格中进行方便定位
        Table aTable = new Table(3);
        // 设置每列所占比例
        // 占页面宽度 90%
        aTable.setWidth(52);
        // 自动填满
        /*aTable.setAutoFillEmptyCells(true);*/
        //这里是imagelist集合，就是图片字节流的集合，图片从流中去获取放到word中
        Point point=new Point(3, 0);
        int flag=0;
        for (int i = 0; i < imageList.size(); i++) {
        	flag=flag+1;
            //设置图片等比例缩小
        	if(flag==2){
        		aTable.addCell(new Cell(""));
        		flag=0;
        	}
        	if(i==2){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==4){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==6){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==8){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
            	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==10){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==12){
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==0 || i==1){
        		imageList.get(i).scalePercent(37);
        		 aTable.addCell(new Cell(imageList.get(i)));
        	}else if(i==12 || i==13){
        		imageList.get(i).scalePercent(30);
        		aTable.addCell(new Cell(imageList.get(i)));
        	}else{
        		imageList.get(i).scalePercent(37);
        		aTable.addCell(new Cell(imageList.get(i)));
        	}
           
            //aTable.addCell(new Cell(""));
        }
        aTable.disableBorderSide(4);
        aTable.setTop(-1);
        document.add(aTable);
        //document.add(new Paragraph("\n"));
        System.out.println("word----success");
        document.close();
        //响应浏览器 返回下载
        response.setContentType("applicaiton/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=" + "dy.doc");
        InputStream is = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        is = new FileInputStream(new File(imgLocation+"/ss.doc"));
        bis = new BufferedInputStream(is);
        os = response.getOutputStream();
        bos = new BufferedOutputStream(os);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = bis.read(b)) != -1) {
            bos.write(b, 0, len);
        }
        bis.close();
        is.close();
        bos.close();
        os.close();
        //删除本地临时文件
        file1.delete();
    }
	public static void createDocContext2(List<Image> imageList,HttpServletRequest request, HttpServletResponse response,String imgLocation) throws DocumentException, IOException {
        File file1 = new File(imgLocation+"/ss.doc");
        //judeFileExists(file1);
        Document document = new Document();
        // 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
        RtfWriter2.getInstance(document, new FileOutputStream(file1));
        document.open();
        //Paragraph title = new Paragraph("登录二维码信息");
        //设置标题格式对齐方式
        //title.setAlignment(Element.ALIGN_CENTER);
        //document.add(title);
        // 设置 Table 表格
        //设置表格，将图片加到表格中进行方便定位
        Table aTable = new Table(2);
        // 设置每列所占比例
        // 占页面宽度 90%
        aTable.setWidth(28);
        // 自动填满
        /*aTable.setAutoFillEmptyCells(true);*/
        //这里是imagelist集合，就是图片字节流的集合，图片从流中去获取放到word中
        Point point=new Point(2, 0);
        int flag=0;
        for (int i = 0; i < imageList.size(); i++) {
        	flag=flag+1;
            //设置图片等比例缩小
        	/*if(flag==2){
        		aTable.addCell(new Cell(""));
        		flag=0;
        	}*/
        	if(i==2){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==4){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==6){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==8){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==10){
        		aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==12){
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==14){
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==16){
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==18){
               	aTable.addCell(new Cell(""));
               	aTable.addCell(new Cell(""));
        	}
        	if(i==0 || i==1){
        		imageList.get(i).scalePercent(26);
        		 aTable.addCell(new Cell(imageList.get(i)));
        	}else if(i==12 || i==13){
        		imageList.get(i).scalePercent(26);
        		aTable.addCell(new Cell(imageList.get(i)));
        	}else{
        		imageList.get(i).scalePercent(26);
        		aTable.addCell(new Cell(imageList.get(i)));
        	}
           
            //aTable.addCell(new Cell(""));
        }
        aTable.disableBorderSide(4);
        aTable.setTop(-1);
        document.add(aTable);
        //document.add(new Paragraph("\n"));
        System.out.println("word----success");
        document.close();
        //响应浏览器 返回下载
        response.setContentType("applicaiton/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=" + "dy.doc");
        InputStream is = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        is = new FileInputStream(new File(imgLocation+"/ss.doc"));
        bis = new BufferedInputStream(is);
        os = response.getOutputStream();
        bos = new BufferedOutputStream(os);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = bis.read(b)) != -1) {
            bos.write(b, 0, len);
        }
        bis.close();
        is.close();
        bos.close();
        os.close();
        //删除本地临时文件
        file1.delete();
    }
}
